package com.cz.ssyx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.ssyx.enums.UserType;
import com.cz.ssyx.enums.user.Leader;
import com.cz.ssyx.enums.user.User;
import com.cz.ssyx.enums.user.UserDelivery;
import com.cz.ssyx.user.mapper.LeaderMapper;
import com.cz.ssyx.user.mapper.UserDeliveryMapper;
import com.cz.ssyx.user.mapper.UserMapper;
import com.cz.ssyx.user.service.UserService;
import com.cz.ssyx.vo.user.LeaderAddressVo;
import com.cz.ssyx.vo.user.UserLoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/24 13:36
 * @version: 1.0
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserDeliveryMapper userDeliveryMapper;

    @Autowired
    private LeaderMapper leaderMapper;

    //根据openid查询user
    @Override
    public User getByOpenid(String openid) {
        return this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getOpenId, openid));
    }

    //根据userId查询提货点和团长信息
    @Override
    public LeaderAddressVo getLeaderAddressByUserId(Long userId) {
        //先查询出用户默认的团长id
        UserDelivery userDelivery = userDeliveryMapper.selectOne(new LambdaQueryWrapper<UserDelivery>()
                .eq(UserDelivery::getUserId, userId)
                .eq(UserDelivery::getIsDefault, 1));

        //如果没有查询出默认的团长，则返回null
        if (userDelivery == null) {
            return null;
        }

        //查询出团长的详细信息
        Leader leader = leaderMapper.selectById(userDelivery.getLeaderId());

        //创建一个团长的vo对象，赋值并返回团长信息
        LeaderAddressVo leaderAddressVo = new LeaderAddressVo();
        BeanUtils.copyProperties(leader, leaderAddressVo);

        leaderAddressVo.setUserId(userId);
        leaderAddressVo.setLeaderId(leader.getId());
        leaderAddressVo.setLeaderName(leader.getName());
        leaderAddressVo.setLeaderPhone(leader.getPhone());
        leaderAddressVo.setWareId(userDelivery.getWareId());
        leaderAddressVo.setStorePath(leader.getStorePath());
        return leaderAddressVo;
    }

    @Override
    public UserLoginVo getUserLoginVo(Long userId) {
        //先查询出用户的基本信息
        User user = this.getById(userId);

        //把基本信息赋值给userLoginVo
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setNickName(user.getNickName());
        userLoginVo.setUserId(userId);
        userLoginVo.setPhotoUrl(user.getPhotoUrl());
        userLoginVo.setOpenId(user.getOpenId());
        userLoginVo.setIsNew(user.getIsNew());

        //判断用户是什么类型
        //如果是团长，则返回团长id和仓库id
        if (user.getUserType() == UserType.LEADER) {
            LambdaQueryWrapper<Leader> leaderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            leaderLambdaQueryWrapper.eq(Leader::getUserId, userId);
            leaderLambdaQueryWrapper.eq(Leader::getCheckStatus, 1);
            Leader leader = leaderMapper.selectOne(leaderLambdaQueryWrapper);
            if (leader != null) {
                userLoginVo.setLeaderId(leader.getId());
//                userLoginVo.setWareId(leader.getRegionId());
                //这里还有一个仓库id
            }
        }else {
            //是会员，返回会员的团长id和仓库id
            LambdaQueryWrapper<UserDelivery> userDeliveryLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userDeliveryLambdaQueryWrapper.eq(UserDelivery::getUserId, userId);
            userDeliveryLambdaQueryWrapper.eq(UserDelivery::getIsDefault, 1);
            UserDelivery userDelivery = userDeliveryMapper.selectOne(userDeliveryLambdaQueryWrapper);
            if (userDelivery != null) {
                userLoginVo.setLeaderId(userDelivery.getLeaderId());
                userLoginVo.setWareId(userDelivery.getWareId());
            }else {
                userLoginVo.setLeaderId(1L);
                userLoginVo.setWareId(1L);
            }
        }
        return userLoginVo;
    }

}
